<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/19
 * Time: 下午6:00
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Modules\Relationship\Models\RsRelationshipModel;
use App\Modules\Relationship\Models\RsRelationshipLevelModel;
use Carbon\Carbon;

class Friends extends Base
{
    public $table = 'friends_follow';
    protected $rsTable = "rs_relationship";
    protected $visitHistory = 'user_visit_history';//拜访记录表

    private $redis_friends_follow = "FRIENDS:FOLLOW:USER"; // 查询是否关注该用户
    private $redis_friends_intimate = "FRIENDS:FOLLOW:INTIMATE"; // 当日好友亲密值
    private $redis_friends_number = "FRIENDS:FOLLOW:NUMBER"; // 关注好友新增数提示
    private $redis_user_visitor_number = "USER:VISITOR:NUMBER";
    private $redis_user_visitor_time_limit = "USER:VISITOR:TIME:LIMIT:";
    private $rsRelationshipModel;
    private $rsRelationshipLevelModel;
    protected $rsFields = array(
        'user_id as userId',
        'friend_id as friendId',
        'type',
        'rs_code as rsCode',
        'intimate'
    );

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->rsRelationshipModel = new RsRelationshipModel();
        $this->rsRelationshipLevelModel = new RsRelationshipLevelModel();
    }

    /**
     * 获取用户关注列表 limit
     * @param $user_id    用户Id
     * @param $offset 启始页面ID, 从0开始
     * @param $limit  页面数量
     * @return mixed
     */
    public function get_follows_limit($user_id, $offset, $limit){
        $follows = DB::table('friends_follow')
            //->leftJoin('user', 'friends_follow.follow_user_id', '=', 'user.user_id')
            ->where('friends_follow.user_id', $user_id)
            ->orderBy('friends_follow.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['friends_follow.follow_user_id as user_id']);
        $result = self::objToArray($follows);
        return $result;
    }

    /**
     * 获取用户关注列表
     * @param $user_id
     * @return mixed
     */
    public function get_follows($user_id){
        $follows = DB::table('friends_follow')
            //->leftJoin('user', 'friends_follow.follow_user_id', '=', 'user.user_id')
            ->where('friends_follow.user_id', $user_id)
            ->orderBy('friends_follow.create_time', 'desc')
            ->get(['friends_follow.follow_user_id as user_id']);
        $result = self::objToArray($follows);
        return $result;
    }

    /**
     * 获取关注数量
     * @param $user_id
     * @return int
     */
    public function get_follows_count($user_id){

        $follows_count = DB::table('friends_follow')
            ->where('user_id', $user_id)
            ->count();
        return $follows_count;
    }

    /**
     * 是否关注
     * @param $user_id
     * @param $follow_user_id
     * @return bool
     */
    public function get_follow_exist($user_id, $follow_user_id){
        $follows_count = DB::table('friends_follow')
            ->where('user_id', $user_id)
            ->where('follow_user_id', $follow_user_id)
            ->count();
        if ($follows_count > 0){
            return true;
        }
        return false;
    }


    /**
     * 获取用户粉丝列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_fans_limit($user_id, $offset, $limit){
        $fans = DB::table('friends_follow')
            //->leftJoin('user', 'friends_follow.user_id', '=', 'user.user_id')
            ->where('friends_follow.follow_user_id', $user_id)
            ->orderBy('friends_follow.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['friends_follow.user_id as user_id']);
        $result = self::objToArray($fans);
        return $result;
    }

    /**
     * 获取用户粉丝列表
     * @param $user_id
     * @return mixed
     */
    public function get_fans($user_id){
        $fans = DB::table('friends_follow')
            //->leftJoin('user', 'friends_follow.user_id', '=', 'user.user_id')
            ->where('friends_follow.follow_user_id', $user_id)
            ->orderBy('friends_follow.create_time', 'desc')
            ->get(['friends_follow.user_id as user_id']);
        $result = self::objToArray($fans);
        return $result;
    }

    /**
     * 获取用户粉丝数
     * @param $user_id
     * @return int
     */
    public function get_fans_count($user_id){
        $fans_count = DB::table('friends_follow')
            ->where('follow_user_id', $user_id)
            ->count();
        return $fans_count;
    }


    /**
     * 获取用户浏览列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_browses_limit($user_id, $offset, $limit){
        $browses = DB::table('friends_browse')
            //->leftJoin('user', 'friends_browse.browse_user_id', '=', 'user.user_id')
            ->where('friends_browse.user_id', $user_id)
            ->orderBy('friends_browse.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['friends_browse.browse_user_id as user_id', 'friends_browse.create_timestamp as create_timestamp']);
        $result = self::objToArray($browses);
        return $result;
    }

    /**
     * 获取用户浏览列表
     * @param $user_id
     * @return mixed
     */
    public function get_browses($user_id){
        $browses = DB::table('friends_browse')
            //->leftJoin('user', 'friends_browse.browse_user_id', '=', 'user.user_id')
            ->where('friends_browse.user_id', $user_id)
            ->orderBy('friends_browse.create_time', 'desc')
            ->get(['friends_browse.browse_user_id as user_id', 'friends_browse.create_timestamp as create_timestamp']);
        $result = self::objToArray($browses);
        return $result;
    }

    /**
     * 获取用户浏览列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_union_browses_limit($user_id, $offset, $limit){
        $browses = DB::table('friends_browse')
            //->leftJoin('user', 'friends_browse.browse_user_id', '=', 'user.user_id')
            ->where('friends_browse.user_id', $user_id)
            ->orderBy('id', 'desc')
            ->groupBy('browse_user_id')
            ->select(DB::raw('max(id) as id'), 'friends_browse.browse_user_id as user_id', 'create_timestamp')
            ->offset(($offset) * $limit)
            ->limit($limit)
            ->get();
        $result = self::objToArray($browses);
        return $result;
    }

    /**
     * 获取用户浏览列表
     * @param $user_id
     * @return mixed
     */
    public function get_union_browses_count($user_id){
        $browses_count = DB::table('friends_browse')
            ->leftJoin('user', 'friends_browse.browse_user_id', '=', 'user.user_id')
            ->where('friends_browse.user_id', $user_id)
            ->groupBy("friends_browse.browse_user_id")
            ->count();
        return $browses_count;
    }

    /**
     * 获取用户浏览数量
     * @param $user_id
     * @return int
     */
    public function get_browses_count($user_id){
        $browses_count = DB::table('friends_browse')
            ->leftJoin('user', 'friends_browse.browse_user_id', '=', 'user.user_id')
            ->where('friends_browse.user_id', $user_id)
            ->count();
        return $browses_count;
    }



    /**
     * 获取用户访客列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_visitors_limit($user_id, $offset, $limit){
        $visitors = DB::table('friends_browse')
            //->leftJoin('user', 'friends_browse.user_id', '=', 'user.user_id')
            ->where('friends_browse.browse_user_id', $user_id)
            ->orderBy('friends_browse.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['friends_browse.user_id as user_id', 'friends_browse.create_timestamp as create_timestamp']);
        $result = self::objToArray($visitors);
        return $result;
    }

    /**
     * 获取用户访客列表
     * @param $user_id
     * @return mixed
     */
    public function get_visitors($user_id){
        $visitors = DB::table('friends_browse')
            //->leftJoin('user', 'friends_browse.user_id', '=', 'user.user_id')
            ->where('friends_browse.browse_user_id', $user_id)
            ->orderBy('friends_browse.create_time', 'desc')
            ->get(['friends_browse.user_id as user_id', 'friends_browse.create_timestamp as create_timestamp']);

        $result = self::objToArray($visitors);
        return $result;
    }

    /**
     * 获取用户访客数量
     * @param $user_id
     * @return int
     */
    public function get_visitors_count($user_id){
        $visitors_count = DB::table('friends_browse')
            ->leftJoin('user', 'friends_browse.user_id', '=', 'user.user_id')
            ->where('friends_browse.browse_user_id', $user_id)
            ->count();
        return $visitors_count;
    }


    /**
     * 好友列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_friends_limit($user_id, $offset, $limit){
        $friends = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.follow_user_id', '=', 'b.user_id')
            //->leftjoin('user as c', 'a.follow_user_id', 'c.user_id')
            ->where('a.user_id', $user_id)
            ->where('b.follow_user_id', $user_id)
            ->orderBy('a.intimate', 'desc')
            ->orderBy('a.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['a.follow_user_id as user_id','a.intimate as intimate']);

        $result = self::objToArray($friends);
        return $result;
    }

    /**
     * 好友列表
     * @param $user_id
     * @return mixed
     */
    public function get_friends($user_id){
        $friends = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.follow_user_id', '=', 'b.user_id')
            //->leftjoin('user as c', 'a.follow_user_id', 'c.user_id')
            ->where('a.user_id', $user_id)
            ->where('b.follow_user_id', $user_id)
            ->orderBy('a.create_time', 'desc')
            ->get(['a.follow_user_id as user_id']);

        $result = self::objToArray($friends);
        return $result;
    }


    /**
     *
     * @param $user_id
     * @param $friends_user_id
     * @return bool
     */
    public function is_friends($user_id, $friends_user_id){
        $friends = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.follow_user_id', '=', 'b.user_id')
            //->leftjoin('user as c', 'a.follow_user_id', 'c.user_id')
            ->where('a.user_id', $user_id)
            ->where('b.follow_user_id', $user_id)
            ->where('a.follow_user_id', $friends_user_id)
            ->where('b.user_id', $friends_user_id)
            ->first(['a.follow_user_id as user_id']);
        if (empty($friends)){
            return false;
        }
        return true;
    }

    /**
     * 好友列表 数量
     * @param $user_id
     * @return int
     */
    public function get_friends_count($user_id){
        $friends_count = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.follow_user_id', '=', 'b.user_id')
            ->where('a.user_id', $user_id)
            ->where('b.follow_user_id', $user_id)
            ->count();
        return $friends_count;
    }


    /**
     * 添加关注数据
     * @param $user_id
     * @param $follow_id
     * @return boolean
     */
    public function add_follow($user_id, $follow_id){
        //判断是会否已经关注过
        if($this->get_follow_exist_redis($user_id ,$follow_id)){
            return 0;
        }
        $ret = DB::table("friends_follow")->insert(['user_id'=>$user_id, 'follow_user_id'=>$follow_id, 'level'=>1,'create_timestamp' => time()]);
        Redis::HINCRBY($this->redis_friends_number, $follow_id, 1); //更新粉丝未读数
        //添加关注更新redis
        $redis_friends_follow_key = $this->redis_friends_follow . ":" . $user_id . ":" . $follow_id;
        Redis::SET($redis_friends_follow_key, 1);
        Redis::EXPIRE($redis_friends_follow_key, 10);
        //更新结束
        return $ret;
    }

    /**
     * 删除关注数据
     * @param $user_id
     * @param $follow_id
     */
    public function del_follow($user_id, $follow_id){
        DB::table("friends_follow")
            ->where("user_id", '=', $user_id)
            ->where("follow_user_id", '=', $follow_id)
            ->delete();
        $redis_friends_follow_key = $this->redis_friends_follow . ":" . $user_id . ":" . $follow_id;
        Redis::del($redis_friends_follow_key);
    }

    /**
     * 添加浏览记录
     * @param $user_id
     * @param $browse_id
     */
    public function add_browse($user_id, $browse_id){
        if ($user_id == $browse_id){
            return ;
        }

        if (Redis::GET($this->redis_user_visitor_time_limit . $user_id . ":" . $browse_id)) {
            return false;
        }

        DB::beginTransaction();
//        DB::table("friends_browse")->where(['user_id'=>$user_id, 'browse_user_id'=>$browse_id])->delete();
        DB::table("friends_browse")->insert(['user_id'=>$user_id, 'browse_user_id'=>$browse_id, 'create_timestamp' => time()]);
        DB::commit();

        Redis::HINCRBY($this->redis_user_visitor_number, $browse_id, 1); //更新访客未读数
        Redis::SET($this->redis_user_visitor_time_limit . $user_id . ":" . $browse_id, $browse_id, 'EX', 3600);
    }



    /**
     * 删除浏览记录
     * @param $user_id
     * @param $browse_id
     */
    public function del_browse($user_id, $browse_id){
        DB::table("friends_browse")
            ->where("user_id", "=", $user_id)
            ->where("browse_id", "=", $browse_id)
            ->delete();
    }



    /**
     * 黑名单列表 limit
     * @param $user_id
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function get_blocks_limit($user_id, $offset, $limit){
        $blocks = DB::table('friends_block')
            //->leftjoin('user', 'friends_block.block_user_id', 'user.user_id')
            ->where('friends_block.user_id', $user_id)
            ->orderBy('friends_block.create_time', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get(['friends_block.block_user_id as user_id']);


        $result = self::objToArray($blocks);
        return $result;
    }

    /**
     * 黑名单列表
     * @param $user_id
     * @return mixed
     */
    public function get_blocks($user_id){
        $blocks = DB::table('friends_block')
            ->leftjoin('user', 'friends_block.block_user_id', 'user.user_id')
            ->where('friends_block.user_id', $user_id)
            ->orderBy('friends_block.create_time', 'desc')
            ->get(['friends_block.block_user_id as user_id']);

        $result = self::objToArray($blocks);
        return $result;
    }

    /**
     * 黑名单数量
     * @param $user_id
     * @return int
     */
    public function get_blocks_count($user_id){
        $blocks_count = DB::table('friends_block')
            ->where('friends_block.user_id', $user_id)
            ->count();
        return $blocks_count;
    }


    /**
     * 黑名单添加
     * @param $user_id
     * @param $block_user_id
     */
    public function add_block($user_id, $block_user_id){
        if (DB::table("friends_block")->where(['user_id'=>$user_id, 'block_user_id'=>$block_user_id])->count() > 0){
            return ;
        }
        DB::table("friends_block")->insert(['user_id'=>$user_id, 'block_user_id'=>$block_user_id, 'create_timestamp' => time()]);
    }


    /**
     * 黑名单删除
     * @param $user_id
     * @param $block_user_id
     */
    public function del_block($user_id, $block_user_id){
        DB::table("friends_block")
            ->where("user_id", "=", $user_id)
            ->where("block_user_id", "=", $block_user_id)
            ->delete();
    }


    /**
     * 是否屏蔽
     * @param $user_id
     * @param $block_user_id
     * @return bool
     */
    public function get_block_exist($user_id, $block_user_id){
        $blocks_count = DB::table('friends_block')
            ->where('user_id', $user_id)
            ->where('block_user_id', $block_user_id)
            ->count();
        if ($blocks_count > 0){
            return true;
        }
        return false;
    }

    //获取用户所有黑名单用户ID
    public function getBlockUserId($userId){
        $list = DB::table('friends_block')
            ->where('user_id', $userId)
            ->select('block_user_id')
            ->get();
        $list = array_column(self::objToArray($list), 'block_user_id');

        return $list;
    }

    /**
     * 获取用户黑名单指定用户
     */
    public function getBlockUserByUserId($user_id, $block_user_id){
        $info = DB::table('friends_block')
            ->where('user_id', $user_id)
            ->where('block_user_id', $block_user_id)
            ->first();
        $info = self::objToArray($info);

        return $info;
    }

    /**
     * 添加用户举报
     * @param $user_id         举报人ID
     * @param $report_user_id  被举报ID
     * @param $report_type     举报类型 1.色情 2.广告 3.诈骗 4.侮辱诋毁 5其他原因
     * @param $pics            图片 字符json
     * @param $describe        举报说明
     */
    public function add_report($user_id, $report_user_id, $report_type, $pics, $describe){

        $pic_arr = explode(",", $pics);
        $images = [];
        for ($i=0; $i<count($pic_arr); $i++){
            $uri_arr = parse_url($pic_arr[$i]);
            if(!empty($uri_arr['path'])){
                $images[] = $uri_arr['path'];
            }
        }
        $pics = json_encode($images);

        DB::table("friends_report")
            ->insert(["user_id"=>$user_id, "report_user_id"=>$report_user_id, "report_type"=>$report_type, "pics"=>$pics, "describe"=>$describe, 'create_timestamp' => time()]);
    }

    /**
     * 获取是否关注-redis版本
     * @param $user_id
     * @param $follow_user_id
     * @return bool
     */
    public function get_follow_exist_redis($user_id, $follow_user_id){
        $redis_friends_follow_key = $this->redis_friends_follow . ":" . $user_id . ":" . $follow_user_id;

        $result = Redis::GET($redis_friends_follow_key);
        if ($result != null) {
            return (int)$result;
        }

        $follows_count = DB::table('friends_follow')
            ->where('user_id', $user_id)
            ->where('follow_user_id', $follow_user_id)
            ->count();

        $data = ($follows_count > 0) ? 1 : 0;
        Redis::SET($redis_friends_follow_key, $data);
        Redis::EXPIRE($redis_friends_follow_key, 10);

        return $data;
    }


    /**
     * 获取好友亲密度
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendIntimate($user_id, $follow_user_id){
        $follows = DB::table('friends_follow')
            ->where('user_id', $user_id)
            ->where('follow_user_id', $follow_user_id)
            ->first(["intimate"]);
        $result = self::objToArray($follows);
        return ($result && $result['intimate']) ? $result['intimate'] : 0;
    }

    /**
     * 获取好友亲密度
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendIntimateReturnNull($user_id, $follow_user_id){
        $follows = DB::table('friends_follow')
            ->where('user_id', $user_id)
            ->where('follow_user_id', $follow_user_id)
            ->first(["intimate"]);
        if (!$follows) {
            return null;
        }
        $result = self::objToArray($follows);
        return $result['intimate'] ? $result['intimate'] : 0;
    }

    /**
     * 获取今日好友聊天亲密度
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendImIntimate($user_id, $follow_user_id){
        return rand(10, 60);
    }

    /**
     * 获取今日好友礼物亲密度
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendGiftIntimate($user_id, $follow_user_id){
        $redis_friends_intimate = $this->redis_friends_intimate . ":2:".date("Ymd");
        return $redisIntimate = (int)Redis::HGET($redis_friends_intimate, "$user_id:$follow_user_id");
    }

    /**
     * 更改好友亲密度
     * @param int $user_id
     * @param int $follow_user_id
     * @param int $type 亲密值增加规则 1 对话  2 送礼物
     * @param int $value 亲密度
     * @return boolean
     *
     * 亲密值细则
        1.对对话：每天对话5次+5
        1.1对话：一条发送和一条回复为对话一次，只计发送和收到条数的最小值即可，如发100
        条，收到20条，则对话了20次；若对话3次则加3点。
        1.2对话亲密值增长每日上限50
     *
        2.送礼物：每赠送/收到100金豆礼物+10
        每赠送/收到10钻石礼物+10
        2.1礼物亲密值增长当日上限100
     *
     *  3.陪伴:每满3分钟增长1亲密度
     *  3.1 每天上限为100
     */

    public function incIntimate($user_id, $follow_user_id, $type, $value){

        if ($type == 2 || $type == 4 || $type == 5) {

            //最大上限1000
            $intimate = $this->getFriendIntimate($user_id, $follow_user_id);
            if ($intimate > 1000) {
                return true;
            }

            //单次上限 100
            if($type != 5){
                $value = $value > 100 ? 100 : $value;
            }
            //当日累计 上限 100
            $redis_friends_intimate = $this->redis_friends_intimate . ":$type:".date("Ymd");


            $redisIntimate = Redis::HINCRBY($redis_friends_intimate, "$user_id:$follow_user_id", $value);
            if(Redis::TTL($redis_friends_intimate) == -1){
                Redis::EXPIRE($redis_friends_intimate, 24 * 60 * 60);
            }

            //好友亲密度
            $redis_friends_follow_intimate = $this->redis_friends_intimate . ":$type:".date("Ymd");
            $redisFollowIntimate = Redis::HINCRBY($redis_friends_follow_intimate,  "$follow_user_id:$user_id", $value);

            if(Redis::TTL($redis_friends_follow_intimate) == -1){
                Redis::EXPIRE($redis_friends_follow_intimate, 24 * 60 * 60);
            }
        }

        if ($type == 4 || $type == 5) {
            # 金豆增长

        }
        return true;
    }

    // 更新好友等级
    public function updateFollowLevel ($user_id, $follow_user_id) {
        // 查总积分
        $re1 = DB::table("friends_follow")
            ->where("user_id", $user_id)
            ->where("follow_user_id",$follow_user_id)
            ->first();
        $re2 = DB::table("friends_follow")
            ->where("user_id", $follow_user_id)
            ->where("follow_user_id",$user_id)
            ->first();
        if (!$re1 || !$re2) {
            return false;
        }
        $levelRow = $this->rsRelationshipLevelModel->getLevelByIntimate($re1->intimate,1);
        if ($levelRow) {
            DB::table("friends_follow")
                ->where("id", $re1->id)
                ->update(["level"=>$levelRow[0]->level]);
            DB::table("friends_follow")
                ->where("id", $re2->id)
                ->update(["level"=>$levelRow[0]->level]);
        }
    }

    /**
     * 获取房间互为好友列表
     * @param $room_id
     * @return boolean
     */
    /**
     * 根据uids获取互关好友列表
     * @param $uids
     * @return array
     */
    public function getFriendsListByUids($uids)
    {
        //获取互为好友列表
        $list = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.user_id', '=', 'b.follow_user_id')
            ->where('a.follow_user_id', '=', DB::raw('zb_b.user_id'))
            ->whereIn('a.user_id', $uids)
            ->whereIn('a.follow_user_id', $uids)
            ->get();

        return self::objToArray($list);
    }

    /**
     * 更新好友亲密度
     * @param $user_id
     * @param $follow_user_id
     * @param $intimate
     * @return integer
     */
    public function updateFollowIntimate($user_id, $follow_user_id, $intimate){

        //清空当日送礼物亲密度
        if ($intimate === 0) {
            // 2今日好友礼物亲密度、3房间好友亲密度、4道具增加值 当天
            $type = [2,3,4];
            foreach ($type as $key => $value) {
                $redis_friends_intimate = $this->redis_friends_intimate . ":$value:".date("Ymd");
                Redis::HSET($redis_friends_intimate, "$user_id:$follow_user_id", 0);
                Redis::HSET($redis_friends_intimate, "$follow_user_id:$user_id", 0);
            }
        }

        return DB::table("friends_follow")
            ->where("user_id", $user_id)
            ->where("follow_user_id", $follow_user_id)
            ->update(["intimate" => $intimate, "level"=>1]);
    }

    /**
     * 获取访客浏览数
     * @param $user_id
     * @param $browse_id
     * @return int
     */
    public function get_redis_visitor_number($user_id){
        $ret = Redis::HGET($this->redis_user_visitor_number, $user_id);
        return !empty($ret) ? intval($ret) : 0;
    }

    /**
     * 清空访客浏览数
     * @param $user_id
     * @return boolean
     */
    public function reset_redis_visitor_number($user_id){
        return Redis::HDEL($this->redis_user_visitor_number, $user_id);
    }

    /**
     * 获取好友关注数
     * @param $user_id
     * @return int
     */
    public function get_redis_friends_number($user_id){
        $ret = Redis::HGET($this->redis_friends_number, $user_id);
        return !empty($ret) ? intval($ret) : 0;
    }

    /**
     * 清空好友关注数
     * @param $user_id
     * @return boolean
     */
    public function reset_redis_friends_number($user_id){
        return Redis::HDEL($this->redis_friends_number, $user_id);
    }

    /**
     * 获取陪伴增加值 当天
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendRoomIntimate($user_id, $follow_user_id){
        $redis_friends_intimate = $this->redis_friends_intimate . ":3:".date("Ymd");
        return $redisIntimate = (int)Redis::HGET($redis_friends_intimate, "$user_id:$follow_user_id");
    }

    /**
     * 获取道具增加值 当天
     * @param $user_id
     * @param $follow_user_id
     * @return integer
     */
    public function getFriendPropIntimate($user_id, $follow_user_id){
        $redis_friends_intimate = $this->redis_friends_intimate . ":4:".date("Ymd");
        return $redisIntimate = (int)Redis::HGET($redis_friends_intimate, "$user_id:$follow_user_id");
    }



    /**
     * 年度好友相关数据
     * @param $user_id
     * @return int
     */
    public function getFriendYearReportCounts($user_id){
        $data['friends_count'] = DB::table('friends_follow as a')
            ->join('friends_follow as b', 'a.follow_user_id', '=', 'b.user_id')
            ->where('a.user_id', $user_id)
            ->where('b.follow_user_id', $user_id)
            ->where('a.create_timestamp', '<', 1577807999)
            ->count();

        $data['fans_count'] = DB::table('friends_follow')
            ->where('follow_user_id', $user_id)
            ->where('create_timestamp', '<', 1577807999)
            ->count();

        return $data;
    }


    /**
     * 删除好友数据
     * @param $user_id
     * @param $follow_id
     */
    public function delFriendsFollowByUids($uids){
        return DB::table("friends_follow")
            ->whereIn("user_id", $uids)
            ->orWhereIn("follow_user_id", $uids)
            ->delete();
    }


    // 增长亲密值主表
    public function incIntimateRsRelationship ($userId, $friendId, $type, $intimate) {
        $row = $this->getOneRelation($userId, $friendId);
        if (!$row) {
            return false;
        }
        if ($type != $row['type']) {
            return false;
        }
        $re = DB::table($this->rsTable)
            ->where('rs_code', $row['rsCode'])
            ->where("is_deleted", 0)
            ->increment('intimate', $intimate);
        return (bool)$re;
    }

    // 获取某条用户关系记录
    public function getOneRelation($userId, $friendId){
        $condition = array(
            "user_id"=>$userId,
            "friend_id"=>$friendId,
            "is_deleted"=>0
        );

        $result = DB::table($this->rsTable)
            ->where($condition)
            ->select($this->rsFields)
            ->first();
        return $this->objToArray($result);
    }

    //记录私信用户足迹
    public function insertVisitHistory($userId, $toUserId){
        $compareTime = Carbon::now()->subHours(24*7);//近7天/小时
        $visitInfo = DB::table($this->visitHistory)->where(['user_id'=>$userId,'visit_id'=>$toUserId,'type'=>1])->where('visit_time','>=', $compareTime)->first();
        $visitInfo = $this->objToArray($visitInfo);
        if(empty($visitInfo)){
            $insert = [
                'user_id' => $userId,
                'visit_id' => $toUserId,
                'type' => 1,//1用户 2=房间
                'count' => 1,
                'visit_time' => date('Y-m-d H:i:s',time()),
                'created_at' => date('Y-m-d H:i:s',time()),
            ];
            DB::table($this->visitHistory)->insert($insert);
        }else{
            DB::table($this->visitHistory)->where('id', $visitInfo['id'])->increment('count',1);
        }
        return true;
    }
    
}


//DB::connection()->enableQueryLog();#开启执行日志
//DB::getQueryLog()